<?php

namespace App\Http\Controllers;

use App\ModelHasPermission;
use App\User;
use Illuminate\Http\Request;
use Spatie\Permission\Models\Permission;

class UsersController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $list = User::paginate(8);
        /*$ids = [1];
        User::whereNotIn('id', $ids)->delete();*/

        return view('users.index', compact('list'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $user = User::findOrFail(4);

        // $user->givePermissionTo('user.info');


    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $data = $this->validate($request, [
            'name' => 'require',
            'email' => 'require',
            'password' => 'require',
            'auth' => 'require',
        ]);

        if (User::create($data)) {
            return back()->with('success', '新增成功');
        } else {
            return back()->with('danger', '新增失败');
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\User  $user
     * @return \Illuminate\Http\Response
     */
    public function show(User $user)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\User  $user
     * @return \Illuminate\Http\Response
     */
    public function edit(User $user)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\User  $user
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, User $user)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\User  $user
     * @return \Illuminate\Http\Response
     */
    public function destroy(User $user)
    {
        if ($user->delete()) {
            return back()->with('success', '删除成功');
        } else {
            return back()->with('danger', '删除失败');
        }
    }

    public function permissionCreate($id)
    {
        $user = User::findOrfail($id);

        $permissions = Permission::all();

        return view('users.create', compact('user', 'permissions'));
    }

    public function permissionStore(Request $request, $id)
    {
        $user = User::findOrFail($id);

        ModelHasPermission::where('model_id', $user->id)->delete();

        app()['cache']->forget('spatie.permission.cache');

        if (!$permissions = $request->input('permissions')) {
            return redirect()->route('users.index')->with('success', '清除成功');
        }

        $auth = $request->input('auth');

        if ($user->givePermissionTo($permissions) && $user->update(['auth' => $auth])) {
            return redirect()->route('users.index')->with('success', '分配成功');
        } else {
            return redirect()->route('users.index')->with('success', '分配失败');
        }
    }
}
